<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商家" prop="sellerId">
      <el-input v-model="dataForm.sellerId" placeholder="商家"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="creatTime">
      <el-input v-model="dataForm.creatTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="操作员" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="操作员"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="购买数量" prop="buyNumber">
      <el-input v-model="dataForm.buyNumber" placeholder="购买数量"></el-input>
    </el-form-item>
    <el-form-item label="可退数量" prop="returnNumber">
      <el-input v-model="dataForm.returnNumber" placeholder="可退数量"></el-input>
    </el-form-item>
    <el-form-item label="类型(1消费2退货)" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型(1消费2退货)"></el-input>
    </el-form-item>
    <el-form-item label="同住人id" prop="peopleId">
      <el-input v-model="dataForm.peopleId" placeholder="同住人id"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="shopPrice">
      <el-input v-model="dataForm.shopPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="单号id" prop="accountId">
      <el-input v-model="dataForm.accountId" placeholder="单号id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          creatTime: '',
          userId: '',
          shopId: '',
          buyNumber: '',
          returnNumber: '',
          type: '',
          peopleId: '',
          shopPrice: '',
          accountId: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '商家不能为空', trigger: 'blur' }
          ],
          creatTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          shopId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          buyNumber: [
            { required: true, message: '购买数量不能为空', trigger: 'blur' }
          ],
          returnNumber: [
            { required: true, message: '可退数量不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型(1消费2退货)不能为空', trigger: 'blur' }
          ],
          peopleId: [
            { required: true, message: '同住人id不能为空', trigger: 'blur' }
          ],
          shopPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          accountId: [
            { required: true, message: '单号id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelfloorroomshoprecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tHotelFloorRoomShopRecord.sellerId
                this.dataForm.creatTime = data.tHotelFloorRoomShopRecord.creatTime
                this.dataForm.userId = data.tHotelFloorRoomShopRecord.userId
                this.dataForm.shopId = data.tHotelFloorRoomShopRecord.shopId
                this.dataForm.buyNumber = data.tHotelFloorRoomShopRecord.buyNumber
                this.dataForm.returnNumber = data.tHotelFloorRoomShopRecord.returnNumber
                this.dataForm.type = data.tHotelFloorRoomShopRecord.type
                this.dataForm.peopleId = data.tHotelFloorRoomShopRecord.peopleId
                this.dataForm.shopPrice = data.tHotelFloorRoomShopRecord.shopPrice
                this.dataForm.accountId = data.tHotelFloorRoomShopRecord.accountId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelfloorroomshoprecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'creatTime': this.dataForm.creatTime,
                'userId': this.dataForm.userId,
                'shopId': this.dataForm.shopId,
                'buyNumber': this.dataForm.buyNumber,
                'returnNumber': this.dataForm.returnNumber,
                'type': this.dataForm.type,
                'peopleId': this.dataForm.peopleId,
                'shopPrice': this.dataForm.shopPrice,
                'accountId': this.dataForm.accountId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
